package com.mystnihon.freeremote.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragment;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.db.table.SeedboxTable;

public class AddSeedboxFragment extends SherlockFragment {

    EditText mEditTextName;
    EditText mEditTextHost;
    EditText mEditTextSLogin;
    EditText mEditTextSPassword;
    EditText mEditTextSPort;
    EditText mEditTextFLogin;
    EditText mEditTextFPassword;
    EditText mEditTextFPort;
    SeedboxTable mSdb = null;

    public static AddSeedboxFragment newInstance() {
        AddSeedboxFragment f = new AddSeedboxFragment();
        return f;

    }

    public void setSeedbox(SeedboxTable sdb) {
        mSdb = sdb;
        if (sdb != null) {
            mEditTextName.setText(sdb.mName);
            mEditTextHost.setText(sdb.mHost);
            mEditTextSLogin.setText(sdb.seedboxLogin);
            mEditTextSPassword.setText(sdb.seedboxPassword);
            mEditTextSPort.setText(String.valueOf(sdb.seedboxPort));
            mEditTextFLogin.setText(sdb.ftpLogin);
            mEditTextFPassword.setText(sdb.ftpPassword);
            mEditTextFPort.setText(String.valueOf(sdb.ftpPort));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_add_seedbox, null);
        mEditTextName = (EditText) v.findViewById(R.id.fr_add_sdb_name);
        mEditTextHost = (EditText) v.findViewById(R.id.fr_add_sdb_host);
        mEditTextSLogin = (EditText) v.findViewById(R.id.fr_add_sdb_login);
        mEditTextSPassword = (EditText) v.findViewById(R.id.fr_add_sdb_password);
        mEditTextSPort = (EditText) v.findViewById(R.id.fr_add_sdb_port);
        mEditTextFLogin = (EditText) v.findViewById(R.id.fr_add_sdb_ftp_login);
        mEditTextFPassword = (EditText) v.findViewById(R.id.fr_add_sdb_ftp_password);
        mEditTextFPort = (EditText) v.findViewById(R.id.fr_add_sdb_ftp_port);

        ((Button) v.findViewById(R.id.fr_add_sdb_valid)).setOnClickListener(mListener);
        ((Button) v.findViewById(R.id.fr_add_sdb_cancel)).setOnClickListener(mListener);
        return v;
    }

    private View.OnClickListener mListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.fr_add_sdb_valid:
                    save();
                    break;
                case R.id.fr_add_sdb_cancel:
                    getSherlockActivity().finish();
                    break;

                default:
                    break;
            }

        }

    };

    private boolean validate() {
        if (!(mEditTextName.getText().toString().isEmpty() || mEditTextHost.getText().toString().isEmpty() || mEditTextSLogin.getText().toString().isEmpty()
                || mEditTextSPassword.getText().toString().isEmpty() || mEditTextSPort.getText().toString().isEmpty())) {
            return true;
        }
        return false;
    }

    private void save() {

        if (validate()) {
            SeedboxTable sdbt = new SeedboxTable();
            sdbt.mName = mEditTextName.getText().toString();
            sdbt.mHost = mEditTextHost.getText().toString();
            sdbt.seedboxLogin = mEditTextSLogin.getText().toString();
            sdbt.seedboxPassword = mEditTextSPassword.getText().toString();
            sdbt.seedboxPort = Integer.parseInt(mEditTextSPort.getText().toString());
            sdbt.ftpLogin = mEditTextFLogin.getText().toString();
            sdbt.ftpPassword = mEditTextFPassword.getText().toString();
            final String ftpport = mEditTextFPort.getText().toString();
            if (!ftpport.isEmpty()) {
                sdbt.ftpPort = Integer.parseInt(ftpport);
            }
            if (mSdb == null) {
                getSherlockActivity().getContentResolver().insert(SeedboxTable.CONTENT_URI, sdbt.getContentValues());
            } else {
                getSherlockActivity().getContentResolver().update(SeedboxTable.CONTENT_URI, sdbt.getContentValues(), SeedboxTable._ID + "=?", new String[] {
                    String.valueOf(mSdb.mId)
                });
            }
            getSherlockActivity().finish();
        } else {
            Toast.makeText(getSherlockActivity(), R.string.add_seedbox_missing_field, Toast.LENGTH_LONG).show();
        }

    }
}
